package com.kaishengit.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.kaishengit.dao.NewsDao;
import com.kaishengit.pojo.Goal;
import com.kaishengit.pojo.Idea;
import com.kaishengit.pojo.News;
import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.Task;
import com.kaishengit.pojo.User;
import com.kaishengit.service.GoalService;
import com.kaishengit.service.NewsService;
import com.kaishengit.service.ProjectService;
import com.kaishengit.service.TaskService;
import com.kaishengit.service.UserService;

@Controller
@Results({
		@Result(name = "addSuccess", location = "user.action", type = "redirectAction"),
		@Result(name = "savenews", location = "main.action", type = "redirectAction", params = {
				"id", "${id}" }),
		@Result(name = "updateproject", location = "user.action", type = "redirectAction")		

})
public class ProjectAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private String id;
	private ProjectService projectService;
	private UserService userService;
	private NewsService newsService;
	private TaskService taskService;
	private GoalService goalService;
	private List<Goal> goalList;
	
	private String name;
	private String content;
	private NewsDao newsDao;

	private List<User> userList;
	private List<Project> projectList;
	private List<Idea> ideaList;
	private List<News> newsList;
	private List<Task> taskList;

	private User user;
	private Project project;
	private Idea idea;
	private News news;

	@Override
	public String execute() throws Exception {

		return SUCCESS;
	}

	@Action("main")
	public String main() {
		putInSession("id", id);
		user = (User) getFromSession("user");
		project = projectService.finById(id);
		putInSession("project", project);
		
		//goalList=goalService.findByUser(user);
		taskList=taskService.findTaskByUser(user);
		System.out.println(taskList.size());

		newsList = getNewsService().getnewstime(project);
		return SUCCESS;
	}

	// news
	@Action("savenews")
	public String savenews() {
		user = (User) getFromSession("user");
		project = (Project) getFromSession("project");

		newsService.save(news, project, user);
		id = project.getId();
		return "savenews";
	}
	@Action("toupdateproject")
	public String toupdateproject(){
		project = (Project) getFromSession("project");
		return "to";
	}
	@Action("updateproject")
	public String updateproject(){
		project = (Project) getFromSession("project");
		
		project.setName(name);
		project.setContent(content);
		
		projectService.update(project);
		return "updateproject";
		
	}

	// project-------------------------
	@Action("saveproject")
	public String save() {
		projectService.save(project, user.getUsername());
		return "addSuccess";
	}

	@Action("addproject")
	public String add() {
		userList = userService.findAll();
		return "add";
	}

	// set get
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public Idea getIdea() {
		return idea;
	}

	public void setIdea(Idea idea) {
		this.idea = idea;
	}

	public NewsService getNewsService() {
		return newsService;
	}

	@Autowired
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public NewsDao getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}

	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	@Autowired
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	
	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public List<Idea> getIdeaList() {
		return ideaList;
	}

	public void setIdeaList(List<Idea> ideaList) {
		this.ideaList = ideaList;
	}

	public List<Project> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Autowired
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public List<Task> getTaskList() {
		return taskList;
	}

	public void setTaskList(List<Task> taskList) {
		this.taskList = taskList;
	}

	@Autowired
	public void setGoalService(GoalService goalService) {
		this.goalService = goalService;
	}

	public List<Goal> getGoalList() {
		return goalList;
	}

	public void setGoalList(List<Goal> goalList) {
		this.goalList = goalList;
	}
	

}
